package cn.jinjimi.app.custom.ui.adapter;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.jinjimi.base.util.DateUtils;
import cn.jinjimi.base.util.StringUtils;
import cn.jinjimi.app.R;
import cn.jinjimi.app.custom.event.ClueUpdateEvent;
import cn.jinjimi.app.custom.ui.activity.CustomClueDetailActivity;
import cn.jinjimi.app.store.entity.ClueEntity;
import cn.jinjimi.app.main.adapter.recycler.MyBaseRecyclerAdapter;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by Roy
 * Date: 16/12/30
 */
public class CustomClueListAdapter extends MyBaseRecyclerAdapter<ClueEntity, CustomClueListAdapter.ViewHolder> {
    private Context mContext;
    public static final int VIEW_TYPE = 1;

    public CustomClueListAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        super.onCreateViewHolder(parent, viewType);
        if (viewType != VIEW_TYPE) return new ViewHolder(new View(mContext));
        View v = LayoutInflater.from(mContext).inflate(R.layout.item_clue_layout, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        ClueEntity item = getItem(position);
        holder.setData(item);
    }

    @Override
    public int getItemViewType(int position) {
        return VIEW_TYPE;
    }

    @Override
    protected Animator[] getAnimators(View view) {
        return new Animator[]{ObjectAnimator.ofFloat(view, "alpha", 0f, 1f)};
    }

    public class ViewHolder extends MyBaseRecyclerAdapter.MyViewHolder {
        public TextView tvName;
        public TextView tvSave;
        public TextView tvSource;
        public TextView tvTime;
        public TextView tvEvent;
        public ImageView imgNew;
        public RelativeLayout rlContent;

        public ViewHolder(View view) {
            super(view);
            if (view == null) return;
            tvName = (TextView) view.findViewById(R.id.tv_name);
            tvSave = (TextView) view.findViewById(R.id.tv_save);
            tvSource = (TextView) view.findViewById(R.id.tv_source);
            tvTime = (TextView) view.findViewById(R.id.tv_time);
            tvEvent = (TextView) view.findViewById(R.id.tv_event);
            imgNew = (ImageView) view.findViewById(R.id.img_new);
            rlContent = (RelativeLayout) view.findViewById(R.id.rl_content);
        }

        public void setData(final ClueEntity item) {

            if (item != null && rlContent != null) {
                tvName.setText(StringUtils.isEmpty(item.realName) ? item.phone : item.realName);
                if (StringUtils.isEmpty(item.realName) && StringUtils.isEmpty(item.phone))
                    tvName.setText(mContext.getString(R.string.clue_unknow_name));

                tvSave.setVisibility(item.customerId == null ? View.VISIBLE : View.GONE);
                tvTime.setText(DateUtils.formatyyyymmddhhmm(item.createdTime));
                tvEvent.setText(item.event);

                tvSource.setText(String.format(mContext.getString(R.string.clue_source), item.source));

                imgNew.setVisibility(item.isNew ? View.VISIBLE : View.GONE);

                rlContent.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        item.isNew = false;
                        item.updateDB();
                        EventBus.getDefault().post(new ClueUpdateEvent(item));
                        if (imgNew.getVisibility() == View.VISIBLE) {
                            imgNew.setVisibility(View.GONE);
                        }

                        CustomClueDetailActivity.launch(mContext, item);
                    }
                });
            }
        }
    }

}